-- 先把全局函数拦一拦,保险的很呢
local old_CanEntitySeeInDark = CanEntitySeeInDark
function CanEntitySeeInDark(inst,...)
    if inst and inst:IsValid() and inst:HasTag('yuemiheng') then
        return true
    end
    return old_CanEntitySeeInDark(inst,...)
end

local old_CanEntitySeeInStorm = CanEntitySeeInStorm
function CanEntitySeeInStorm(inst,...)
    if inst and inst:IsValid() and inst:HasTag('yuemiheng') then
        return true
    end
    return old_CanEntitySeeInStorm(inst,...)
end

local old_CanEntitySeePoint = CanEntitySeePoint
function CanEntitySeePoint(inst,x,y,z,...)
    if inst and inst:IsValid() and inst:HasTag('yuemiheng') then
        return true
    end
    return old_CanEntitySeePoint(inst,x,y,z,...)
end


local old_CanEntitySeeTarget = CanEntitySeeTarget
function CanEntitySeeTarget(inst,target,...)
    if inst and inst:IsValid() and inst:HasTag('yuemiheng') then
        return true
    end
    return old_CanEntitySeeTarget(inst,target,...)
end

-- local tags = {
--     ['nightvision'] = true,
--     -- ['goggles'] = true,
-- }
-- 然后拦一拦客机部分,确保判断条件 夜视和护目镜 都能通过 
AddClassPostConstruct("components/inventory_replica", function(self)
    local old_EquipHasTag = self.EquipHasTag
    function self:EquipHasTag(tag,...)
        if self.inst:HasTag('yuemiheng') then
            if tag == 'nightvision' then
                return true
            elseif tag == 'goggles' then
                return self.inst:HasTag('ymh_seesandstorm')
            end
        end
        return old_EquipHasTag(self,tag,...)
    end
end)


AddPrefabPostInit("yuemiheng", function(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    -- 拦截判定玩家是否有护目镜 部分,让处于沙暴中时,始终返回true
    local old_HasGoggleVision = inst.components.playervision.HasGoggleVision
    function inst.components.playervision:HasGoggleVision(...)
        if TheWorld and TheWorld.components.sandstorms and TheWorld.components.sandstorms:IsInSandstorm(inst) then 
            return true
        end
        return false
        -- return old_HasGoggleVision(self,...)
    end

    inst:DoPeriodicTask(5,function()
        if TheWorld and TheWorld.components.sandstorms and TheWorld.components.sandstorms:IsInSandstorm(inst) then
            -- 移除移速减益
            inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "sandstorm")
            if not inst:HasTag('ymh_seesandstorm') then
                inst:PushEvent('gogglevision',{enabled = true})
                inst:AddTag('ymh_seesandstorm')
            end
        else
            if inst:HasTag('ymh_seesandstorm') then
                inst:PushEvent('gogglevision',{enabled = false})
                inst:RemoveTag('ymh_seesandstorm')
            end
        end
    end)
end)


AddPlayerPostInit(function(inst)
    if not TheWorld.ismastersim then
        if inst and inst.prefab and inst.prefab == 'yuemiheng' then
            inst:DoTaskInTime(0, function()
                PostProcessor:EnablePostProcessEffect(PostProcessorEffects['ColourCube'], false)
                inst:DoPeriodicTask(5,function()
                    PostProcessor:EnablePostProcessEffect(PostProcessorEffects['ColourCube'], false)
                end)
            end)
        end
    end
end)